package com.ndp.fb.walle.mqconsumer.message;

import com.alibaba.fastjson.JSON;
import com.ndp.fb.constant.ConfigConst;
import com.ndp.fb.model.fbagent.param.ApiCallBackTarget;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.walle.model.vo.eventnotificationmessage.EventNotificationMessageVo;
import com.ndp.fb.walle.mqconsumer.AbstractPullConsumer;
import com.ndp.fb.walle.util.MenuCacheUtils;
import com.ndp.fb.walle.websocket.WebsocketClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by elvisxiong on 7/27/15.
 */
@Component
public class PushNotificationConsumerImpl extends AbstractPullConsumer {

    private static Logger logger = LoggerFactory.getLogger(PushNotificationConsumerImpl.class);
    @Autowired
    WebsocketClient websocketClient;

    @Override
    protected void consume(String tag, String keys, Object message, Map<String, String> equipment) {
        logger.info("MessageConsumerImpl tag >>>> " + tag + " keys >>>" + keys + " message >>> " + message);
        ApiCallBackTarget.WebPush target = (ApiCallBackTarget.WebPush) ApiCallBackTarget.getTargetType(tag);
        switch (target){
            case WebSocketPushNotification:
                try{
                    Object[] objects = (Object[])message;
                    Long id = (Long) objects[0];
                    String userId = (String)objects[1];
                    Integer eventType = (Integer) objects[2];
                    String title = (String) objects[3];
                    String body = (String) objects[4];
                    Boolean isNeedFunctionJump = (Boolean)objects[5];
                    String url = (String) objects[6];
                    Long badageNum = (Long) objects[7];
                    String eventKey = (String) objects[8];
                    String eventValues = (String) objects[9];


                    logger.info("开始推送事件消息到webclient,userId:"+userId +",title:"+title +",body:"+body);
                    EventNotificationMessageVo eventNotificationMessageVo = new EventNotificationMessageVo();
                    eventNotificationMessageVo.setId(id);
                    eventNotificationMessageVo.setReceiver(userId);
                    eventNotificationMessageVo.setBody(body);
                    eventNotificationMessageVo.setIsNeedFunctionJump(isNeedFunctionJump);
                    eventNotificationMessageVo.setIsRead(0);
                    eventNotificationMessageVo.setStatus(1);
                    eventNotificationMessageVo.setUrl(url);
                    eventNotificationMessageVo.setTitle(title);
                    eventNotificationMessageVo.setSendTime(new Date().getTime());
                    eventNotificationMessageVo.setEventKey(eventKey);
                    eventNotificationMessageVo.setEventValues(eventValues);
                    List<EventNotificationMessageVo> resultList = new ArrayList<>();
                    resultList.add(eventNotificationMessageVo);
                    Pager<EventNotificationMessageVo> pager = new Pager<>();
                    pager.setPageIndex(1);
                    pager.setPageSize(1);
                    pager.setResultList(resultList);
                    pager.setTotalSize(badageNum);
                    String json = JSON.toJSONString(pager);
                    logger.info("推送websocket,json:"+json);
                    websocketClient.sendMessageToOne(userId,pager);
                }catch (IOException ex){
                    logger.error(ex.getMessage(),ex);
                }
                break;
            case RoleMenuCacheCleanNotification:
                try{
                    Object[] objects = (Object[])message;
                    String roleId = (String)objects[0];
                    MenuCacheUtils.setMenuCache(roleId,null);
                }catch (Exception e){
                    logger.error(e.getMessage(),e);
                }break;
        }
    }
    @Override
    public String getTopicName() {
        return ConfigConst.MQ_SERVICES_WEB_PUSH_TOPIC;
    }
}
